from tkinter import *
import tkinter.messagebox
from tkinter import colorchooser
from tkinter.colorchooser import *
class DrawP(Frame):
    def __init__(self, master) -> None:
        super().__init__(master=master)
        self.startx=0
        self.starty=0
        self.lastline=None
        self.color="red"
        self.fcolor="red"
        self.type="画线"
        self.creatWidgets()
    def mouseClick(self,event):   #方法
        self.startx=event.x
        self.starty=event.y
    def creatWidgets(self):
        self.cav=Canvas(self,width=800,height=550,bg="white")
        self.cav.bind("<Button-1>",self.mouseClick)
        self.cav.bind("<B1-Motion>",self.mouseMove)
        self.cav.bind("<ButtonRelease-1>",self.mouseRelease)
        self.cav.pack()
        self.btLine=Button(self,text="画线",font="宋体,8")
        self.btLine.bind("<Button-1>",self.btClick)
        self.btLine.pack()
        self.btOval=Button(self,text="画圆",font="宋体,8")
        self.btOval.bind("<Button-1>",self.btClick)
        self.btOval.pack()
        self.btrectangle=Button(self,text="画矩形",font="宋体,8")
        self.btrectangle.bind("<Button-1>",self.btClick)
        self.btrectangle.pack()
        self.btColor=Button(self,text="画线颜色",font="宋体,8")
        self.btColor.bind("<Button-1>",self.btColorClick)
        self.btColor.pack()

    def mouseMove(self,event):
        self.endx=event.x
        self.endy=event.y
        self.cav.delete(self.lastline)
        if(self.type=="画线"):
                self.lastline=self.cav.create_line(self.startx,self.starty,self.endx,self.endy,fill=self.fcolor)
        elif(self.type=="画圆"):
            self.lastline=self.cav.create_oval(self.startx,self.starty,self.endx,self.endy,fill=self.fcolor)
        else:
            self.lastline=self.cav.create_rectangle(self.startx,self.starty,self.endx,self.endy,fill=self.fcolor)
    def mouseRelease(self,event):
        self.lastline=None
    def btClick(self,event):
        self.type=event.widget["text"]
    def btColorClick(self,event):
        s1=askcolor(color="red",title="选择颜色")
        self.fcolor=s1[1]
root=Tk()
root.geometry("800x600")
#创建菜单
application=DrawP(root) 
application.pack()
root.mainloop()




"""menubar=Menu(root)
file_menu =Menu(menubar)
menubar.add_cascade(label="07文件及IO操作",menu=file_menu)
root["menu"]=menubar
application=DrawP(root)
application.pack()
new=DrawP(root)
new.pack()
application.destroy()
root.mainloop()"""